package cn.elegent.txdemo.user.service.impl;
import cn.elegent.tx.annotation.ElegentTx;
import cn.elegent.txdemo.common.dto.PayDto;
import cn.elegent.txdemo.user.entity.User;
import cn.elegent.txdemo.user.mapper.UserMapper;
import cn.elegent.txdemo.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class UserServiceImpl implements UserService {


    @Autowired
    private UserMapper userDao;


    @Override
    @ElegentTx(mainName = "pay",childName = "user")
    public void deduct(PayDto payDto) {
        User user = userDao.selectById(payDto.getUserId());
        user.setAmount( user.getAmount()- payDto.getNum()*payDto.getPrice() );//扣减账户余额
        userDao.updateById(user);
        if(user.getAmount()<0){
            throw new RuntimeException("余额不足！");
        }
    }


}
